<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-03-20
 * Time: 16:30
 */

/**
 * 多级分类函数
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function catlist($cate,$pname='pid',$catename='_cate_name',$pid=0,$html1='|',$html2='—',$level=0){
    $arr=array();
    $html='';
    foreach($cate as $k=>$v){
        if($v->$pname==$pid){
            $v['level']=$level+1;
            if($level==1){
                $html=$html1.$html2;
            }
            if($level>1){
                $html=$html1.str_repeat($html2,$level);;
            }
            $v[$catename]=$html.$v->name;
            $arr[]=$v;
            $v['level']=$level+1;
            $arr=array_merge($arr,catlist($cate,$pname,$catename,$v->id,$html1,$html2,$level+1));
        }
    }
    return $arr;
}

/**
 * 权限多级分类函数
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2017-03-27
 */
function pertree($cate,$pname='pid',$catename='_cate_name',$pid=0,$html1='|',$html2='—',$level=0){
    $arr=array();
    $html='';
    foreach($cate as $k=>$v){
        if($v->$pname==$pid){
            $v['level']=$level+1;
            if($level==1){
                $html=$html1.$html2;
            }
            if($level>1){
                $html=$html1.str_repeat($html2,$level);;
            }
            $v[$catename]=$html.ucfirst($v->display_name);
            $arr[]=$v;
            $v['level']=$level+1;
            $arr=array_merge($arr,pertree($cate,$pname,$catename,$v->id,$html1,$html2,$level+1));
        }
    }
    return $arr;
}

/**
 * 多级分类树形
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function cattree($cate,$pname='pid',$pid=0){
    $arr=array();
    foreach($cate as $k=>$v){
        if($v->$pname==$pid){
            $v['sub']=cattree($cate,$pname,$v->id);
            $arr[]=$v;
        }
    }
    return $arr;
}

/**
 * 内容用多级分类树形
 * @param
 * @acthor:式神 289650682@qq.com
 * @return 2016-10-12
 */
function catecontree($cate,$pname='pid',$pid=0,$spread=true){
    $arr=array();
    foreach($cate as $k=>$v){
        $v['spread']=$spread;
        $v->name=str_replace("&"," ",$v->name);
        if($v->$pname==$pid){
            $v['children']=catecontree($cate,$pname,$v->id);
            $arr[]=$v;
        }
    }
    return $arr;
}

/**
 * 查询子栏目id
 * @acthor:式神 2896506822qq.com
 * @time: 2016-10-12
 */
//子栏目ID
function getChildsId($cate,$pid=0,$pname='pid'){
    $arr=array();
    foreach ($cate as $v) {
        if($v->$pname==$pid){
            $arr[]=$v->id;
            $arr= array_merge($arr,getChildsId($cate,$v->id,$pname));
        }
    }
    return $arr;
}


function qtree($cate,$pname='pid',$pid=0){
    $arr=array();
    foreach($cate as $k=>$v){
        if($v->type==2){
            $v['url']=$v->links;
        }else{
            $v['url']=url('').'/list/'.$v->id;
        }
        if($v->$pname==$pid){
            $v['sub']=qtree($cate,$pname,$v->id);
            $arr[]=$v;
        }
    }
    return $arr;
}

/*function links($num,$links,$id){
    $url='';
    if($num==2){
        $url=$links;
    }else{
        $url=url('').'/list/'.$id;
    }
    return $url;
}*/